Page({
    data: {
        navToggle: false,
        scrollTop: 0,
        userInfo: null,
        isLoading: true,
        merchantInfo: {}, // 商家信息
        salesData: {}, // 销售数据
    },
    onShow() {
        this.getTabBar().init();
        this.setData({
            scrollTop: 0,
            navToggle: false,
            userInfo: wx.getStorageSync('userInfo'),
        });
        // 获取今日销售业绩
        this.fetchTodaySales()
        if (this.data.userInfo && this.data.userInfo._openid) {
            // 获取商家信息
            this.fetchMerchantInfo()
        }
    },
    navigateToSimulateUser() {
        wx.navigateTo({
            url: '/pages/simulate-user/index', // 路径根据你的实际页面路径调整
        });
    },
    // 获取商家信息
    fetchMerchantInfo() {
        wx.cloud.callFunction({
            name: 'merchantInfo',
            data: {
                type: "getMerchantInfo",
                data: {
                    conditions: {
                        _openid: this.data.userInfo._openid,
                        userId: this.data.userInfo.userId,
                    }
                }
            }
        }).then(res => {
            // console.log("res----", res)
            if (res.result.code == 200) {
                this.setData({
                    merchantInfo: res.result.data
                })
                wx.setStorageSync('userInfo', res.result.data)
            }
        }).catch(err => {
            console.log("err----", err)
        })
    },
    // 获取今日销售业绩
    fetchTodaySales() {
        wx.cloud.callFunction({
            name: "getPageData",
            data: {
                type: "getTodaySales",
                data: {
                    conditions: {
                        _openid: this.data.userInfo._openid,
                    }
                }
            }
        }).then(res => {
            // console.log("res----", res)
            if (res.result.code == 200) {
                this.setData({
                    salesData: {
                        ...res.result.data,
                        todaySales: res.result.data.todaySales.toFixed(2),
                        memberSales: res.result.data.memberSales.toFixed(2),
                        crossBorderMemberSales: res.result.data.crossBorderMemberSales.toFixed(2),
                        totalAmount: res.result.data.totalAmount.toFixed(2),
                    },
                    isLoading: false,
                })
            }
        }).catch(err => {
            console.log("err===", err)
        })
    },
    // 滑动到顶部触发
    upper() {
        this.setData({
            navToggle: false,
        });
    },
    // 监听滚动
    scroll(e) {
        if (e.detail.scrollTop > 40) {
            // 已滑到指定位置
            this.setData({
                navToggle: true,
            });
        } else if (e.detail.scrollTop < 40) {
            // 未滑到指定位置
            this.setData({
                navToggle: false,
            });
        }
    },
    // 消息通知
    toMsg() {
        if (this.data.userInfo && this.data.userInfo._openid) {
            wx.navigateTo({
                url: "/subPackages/msg/list/index",
            });
        } else {
            wx.showToast({
                title: '请登录',
                icon: 'none'
            })
        }
    },
    // 跳转全部
    toColumnAll(e) {
        const {
            value
        } = e.currentTarget.dataset;
        switch (value) {
            case "1":
                // 销售管理汇总
                wx.navigateTo({
                    url: "/subPackages/homePackage/salesManage/index",
                });
                break;
            case "2":
                // 日常客户管理

                break;
            case "3":
                // 商家信息管理

                break;
            case "4":
                // 商品管理

                break;
            default:
                return;
        }
    },
    // 跳转会员数量
    toRoleStats() {
        wx.navigateTo({
            url: '/subPackages/homePackage/memberStats/index/index',
        })
    },
    // 切换角色
    switchRole(e) {
        const {
            role
        } = e.currentTarget.dataset
        // 存储数据角色
        wx.setStorageSync('dataRole', role)
        wx.switchTab({
            url: `/pages/data/index`,
        })
    },
    // 商家信息管理跳转
    merchantInfoSkip(e) {
        const {
            value
        } = e.currentTarget.dataset;
        switch (value) {
            case "1":
                // 我的订单
                wx.navigateTo({
                    url: "/subPackages/my/myOrders/index",
                });
                break;
            case "2":
                // 收货地址
                wx.navigateTo({
                    url: "/subPackages/my/address/list/index",
                });
                break;
            case "3":
                // 我的收藏
                wx.navigateTo({
                    url: "/subPackages/my/collection/index",
                });
                break;
            case "4":
                // 我的足迹
                wx.navigateTo({
                    url: "/subPackages/my/footMark/index",
                });
                break;
            case "5":
                // 在线客服
                break;
            case "6":
                // 帮助中心
                wx.navigateTo({
                    url: "/subPackages/my/help/index",
                });
                break;
            case "7":
                // 意见反馈
                wx.navigateTo({
                    url: "/subPackages/my/feedback/index",
                });
                break;
            default:
                return;
        }
    },
    // 跳转商品管理
    toGoodsManage(e) {
        wx.navigateTo({
            url: '/subPackages/homePackage/goodsManage/list/index',
        })
    },
    // 跳转优惠券管理
    toCouponManage() {
        wx.navigateTo({
            url: '/subPackages/homePackage/couponManage/list/index',
        })
    },
    // 优惠券汇总
    toSummarizeCoupons() {
        wx.navigateTo({
            url: '/subPackages/homePackage/couponManage/summary/index',
        })
    },
    // 跳转设置
    toSetting() {
        wx.navigateTo({
            url: "/subPackages/compileLogin/index",
        });
    },
});